package exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.bean.CompanyInfo;
import exam.bean.ProjectInfo;
import exam.bean.UtilFiles;
import exam.dao.CompanyInfoDao;
import exam.dao.UtilFileDao;
import exam.service.ICompanyInfoService;
import exam.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyInfoServiceImpl implements ICompanyInfoService {

    @Autowired
    CompanyInfoDao companyInfoDao;

    @Autowired
    UtilFileDao utilFileDao;

    @Override
    public PageInfo<CompanyInfo> selectInfo(int pageNum, int pageSize, Map<String, Object> queryMap) {
        PageHelper.startPage(pageNum,pageSize);
        //查询附件
        List<CompanyInfo> res = companyInfoDao.selectInfo(queryMap);
        for (CompanyInfo companyInfo : res) {
            Map<String,Object> map = new HashMap<>();
            map.put("utilType",4);
            map.put("dataId",companyInfo.getId());
            companyInfo.setFiles(utilFileDao.selectInfo(map));
        }
        return new PageInfo<>(res);
    }

    @Override
    public CompanyInfo selectInfoSingle(Map<String, Object> queryMap) {
        List<CompanyInfo> resList = companyInfoDao.selectInfo(queryMap);
        if(resList != null && resList.size() !=0){
            //        附件查询
            CompanyInfo companyInfo = resList.get(0);
            Map<String,Object> map = new HashMap<>();
            map.put("utilType",4);
            map.put("dataId",queryMap.get("id"));
            companyInfo.setFiles(utilFileDao.selectInfo(map));
         return companyInfo;
        }else {
            return null;
        }
    }

    @Override
    public int insertInfo(CompanyInfo companyInfo,String uploadPath) {
        int inInfo = companyInfoDao.insertInfo(companyInfo);
        int fileRe = 0;
        if(inInfo > 0 && companyInfo.getUtilFiles() != null){
            for (MultipartFile file:companyInfo.getUtilFiles()) {
                UtilFiles files = new UtilFiles();
                files.setDataId(companyInfo.getId());
                files.setUtilName(FileUtil.upLoadFileSingle(file,uploadPath));
                files.setUtilAddress(uploadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(4);
                fileRe = utilFileDao.insertInfo(files);
            }
        }
        return fileRe + inInfo;
    }

    @Override
    public int updateInfo(CompanyInfo companyInfo,String uploadPath) {

        int fileRe = 0;
        if(companyInfo.getUtilFiles() != null){
            for (MultipartFile file:companyInfo.getUtilFiles()) {
                UtilFiles files = new UtilFiles();
                files.setDataId(companyInfo.getId());
                files.setUtilName(FileUtil.upLoadFileSingle(file,uploadPath));
                files.setUtilAddress(uploadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(4);
                fileRe = utilFileDao.insertInfo(files);
            }
        }
        return companyInfoDao.updateInfo(companyInfo) + fileRe;
    }

    @Override
    public int deleteInfo(Integer id) {
        return companyInfoDao.deleteInfo(id);
    }

    @Override
    public List<Map<String, Object>> searchInput(String companyName) {
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("companyName",companyName);
        //查询所有满足条件的数据
        List<CompanyInfo> resList = companyInfoDao.selectInfo(queryMap);
        //将其封装为map => id,companyName
        List<Map<String,Object>> results = new ArrayList<>();
        for (CompanyInfo companyInfo:resList) {
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id",companyInfo.getId());
            dataMap.put("companyName",companyInfo.getCompanyName());
            results.add(dataMap);
        }
        return results;
    }
}
